//
//  QRCreatInputViewController.m
//  EducationOID
//
//  Created by slz on 2017/6/7.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "QRCreatInputViewController.h"
#import "QRCreatFinishedViewController.h"
#import "NSString+OIDAnalysis.h"
#import "OIDCacheManager.h"

@interface QRCreatInputViewController ()<YYTextViewDelegate>
@property (nonatomic,strong) YYTextView *textView;
@property (nonatomic,strong) UIButton *sureButton;
@property (nonatomic,strong) UILabel *countLabel;
@end

@implementation QRCreatInputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showNavigationBar = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;
    [self setControllerTitle:@"文本"];
    
    _textView = [[YYTextView alloc]initWithFrame:CGRectMake(11, 11, kScreenWidth - 22, 140)];
    _textView.placeholderText = @"请输入信息";
    _textView.delegate = self;
    _textView.keyboardType = _isQrCode ? UIKeyboardTypeDefault : UIKeyboardTypeASCIICapable;
    _textView.font = [UIFont systemFontOfSize:15];
    _textView.layer.borderWidth = 1;
    _textView.layer.borderColor = kLineColor_Gray_Default.CGColor;
    [self.view addSubview:_textView];
    
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sureButton addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [_sureButton setTitle:_isQrCode ? @"生成二维码" : @"生成条形码" forState:UIControlStateNormal];
    [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sureButton.layer.cornerRadius = 3;
    _sureButton.titleLabel.fontSize = 14;
    _sureButton.backgroundColor = kBack_Color_Default;
    [self.view addSubview:_sureButton];
    [_sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(-11);
        make.top.mas_equalTo(_textView.mas_bottom).offset(11);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
    if (!_isQrCode){
        _countLabel = [UILabel new];
        _countLabel.fontSize = 12;
        _countLabel.textColor = kDark_TextColor_Default;
        _countLabel.text = @"您已输入0/13个字";
        [self.view addSubview:_countLabel];
        [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.view.mas_left).offset(11);
            make.top.mas_equalTo(_textView.mas_bottom).offset(11);
        }];
    }
    // Do any additional setup after loading the view.
}



- (void)textViewDidChange:(YYTextView *)textView{
    if (!_isQrCode){
        NSInteger length = textView.text.length;
        _countLabel.text = [NSString stringWithFormat:@"您已输入%ld/13个字",length];
        if(length > 13){
            textView.text = [textView.text substringToIndex:13];
        }
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if ([_textView isFirstResponder]){
        [_textView resignFirstResponder];
    }
}

- (void)sureAction{
    NSString * sourceString = _textView.text;
    BOOL ret = [sourceString includeChinese];
    if ([sourceString trim].length > 0){
        if (ret && !_isQrCode){
            [self.view showTip:@"条形码不支持中文"];
            return;
        }
        [[OIDCacheManager shareInstance] updateWithCreatOidString:_textView.text];
        
        QRCreatFinishedViewController * finishedController = [[QRCreatFinishedViewController alloc]init];
        finishedController.sourceString = _textView.text;
        finishedController.isQrCode = _isQrCode;
        [self.navigationController pushViewController:finishedController animated:YES];
    }else{
        [self.view showTip:@"请输入信息"];
    }
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
